/*
 * main.c
 *
 *  Created on: Aug 31, 2012
 *      Author: javier
 */

#include <stdio.h>
#include <stdlib.h>

char menu() {
	char c;
	printf("\n2 - Binario -> Octal\n");
	printf("8 - Octal -> Binario\n");
	printf("S - Salir\n");

	printf("\nSeleccione opcion: ");
	c = getchar();
	return c;
}

unsigned long conversor(unsigned long n1, int base1, int base2) {
	unsigned long alg, mult = 1, n2 = 0;
	while (n1 > 0) {
		alg = n1 % base1;
		n1 /= base1;
		n2 += (alg * mult);
		mult *= base2;
	}
	return n2;
}

void main() {
	unsigned long n;
	char op;

	for (;;) {
		op = menu();

		switch (op) {
		case '2':
			printf("Conversor Binario -> Octal\n");
			printf("Introduza un numero Binario: ");
			scanf("%lu", &n);
			printf("Numero Octal correspondente: ");
			printf("%lo", conversor(n, 10, 2));
			break;

		case '8':
			printf("Conversor Octal -> Binario\n");
			printf("Introduza un numero Octal: ");
			scanf("%lo", &n);
			printf("Numero Binario correspondente: ");
			printf("%lu", conversor(n, 2, 10));
			break;

		case 'S':
		case 's':
			exit(0);
			break;

		default:
			printf("Opcion invalida!");
		}
		getchar();
	}
}

